using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Chat.metier;
using Chat.dao;

using Spring.Context;
using Spring.Context.Support;
using log4net;
using ChatServer;
using Iesi.Collections;

namespace Chat.service
{
    class UserService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(UserService));
        /// <summary>
        /// Cette methode renvoit un utiisateur a partir de son id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public User getById(int id)
        {
            User user = null;
            try
            {
                IUserDAO p = (IUserDAO)InitFramework.springCtx.GetObject("user.dao");
                user = p.getById(id);
            }
            catch (Exception e1)
            {
                log.Warn("Erreur dans le dao User_getById ", e1);
                return null;
            }
            return user;
        }

        /// <summary>
        /// Cette methode renvoit tous les utilisateurs
        /// </summary>
        /// <returns></returns>
        public IList getAll()
        {
            IList list = null;
            try
            {
                IUserDAO p = (IUserDAO)InitFramework.springCtx.GetObject("user.dao");
                list = p.getAll();
            }
            catch (Exception e1)
            {
                log.Warn("Erreur dans le User_getAll", e1);
                return null;
            }
            return list;
        }

        public bool register(String login, String passwd, String name, String email)
        {
            UserDAO uDAO = new UserDAO();

            if (uDAO.login_exist(login))
                return false;

            User user = new User();

            user.Login = login;
            user.Passwd = passwd;
            user.Name = name;
            user.Email = email;

            uDAO.save(user);

            return true;
        }

        public User login(String login, String passwd)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.login(login, passwd);
        }

        public ISet getContacts(int id)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.getContacts(id);
        }

        public ISet getContacts(User user)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.getContacts(user);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"> The User id</param>
        /// <param name="login"> The Contact's login to add</param>
        /// <returns></returns>
        public bool addContact(int id, String login)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.addContact(id, login);
        }

        /// <summary>
        /// Delete a Contact of a User
        /// </summary>
        /// <param name="idUser"></param>
        /// <param name="idContact"></param>
        /// <returns></returns>
        public bool deleteContact(int idUser, int idContact)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.deleteContact(idUser, idContact);
        }

        public User getUserByGuid(int id)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.getById(id);
        }

        public User getUserByLogin(string login)
        {
            UserDAO uDAO = new UserDAO();
            return uDAO.getByLogin(login);
        }
    }
}
